﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gean;
using Pansoft.HIME.Base.Entities;
using Pansoft.HIME.Base.Enums;

namespace Pansoft.HIME.Repository
{
#if DEBUG

    static class Mocks
    {
        private static readonly UtilityRandom _Rand = new UtilityRandom();

        public static void SetDemoOrganizationTree(ITree<Organization> tree)
        {
            int id = 1;
            var root = GetDemoOrganization(ref id, "", OrganizationCategory.Root);
            tree.AddChild(root);

            var category1 = _Rand.Next(3, 30);
            for (int i = 0; i < category1; i++)
            {
                var category = GetDemoOrganization(ref id, root.Id, OrganizationCategory.Category);
                tree[root].Add(category);
                var item1 = _Rand.Next(2, 30);
                for (int j = 0; j < item1; j++)
                {
                    var item = GetDemoOrganization(ref id, category.Id);
                    tree[category].Add(item);
                }
            }
        }

        /// <summary>创建一个随机生成的组织实体
        /// </summary>
        /// <param name="id"></param>
        /// <param name="parentId"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        public static Organization GetDemoOrganization(ref int id, string parentId, OrganizationCategory category = OrganizationCategory.Item)
        {
            var org = new Organization();
            var nameFlag = string.Empty;
            switch (category)
            {
                case OrganizationCategory.Root:
                    nameFlag = "ROOT";
                    break;
                case OrganizationCategory.Category:
                    nameFlag = "CC";
                    org.ParentId = parentId;
                    break;
                default:
                    org.ParentId = parentId;
                    break;
            }
            org.Id = string.Format("ORG-{0}{1}", nameFlag, id.ToString().PadLeft(3, '0')).ToUpper();
            org.Name = string.Format("组织名称{0}", id.ToString().PadLeft(3, '0'));
            org.Category = category;
            org.CreatedTime = new DateTime(_Rand.Next(1996, 2011), _Rand.Next(1, 12), _Rand.Next(1, 25));
            var jg = DateTime.Now - org.CreatedTime;
            org.ModifiedTime = org.CreatedTime.AddDays(_Rand.Next(1, (int)jg.TotalDays));
            org.CreaterId = string.Format("{0}-{1}", Guid.NewGuid().ToString("N").Substring(0, 3), _Rand.Next(0, 50).ToString().PadLeft(3, '0'));
            org.MenderId = string.Format("{0}-{1}", Guid.NewGuid().ToString("N").Substring(0, 3), _Rand.Next(0, 50).ToString().PadLeft(3, '0'));
            org.IsDeleted = false;

            id++;

            return org;
        }
    }

#endif
}
